/*
 * @Author: 颜礼田 2564217790@qq.com
 * @Date: 2024-09-07 09:49:20
 * @LastEditors: 颜礼田 2564217790@qq.com
 * @LastEditTime: 2024-09-07 18:09:05
 * @FilePath: /webServer/src/utils/resHelper.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// !定义状态码枚举
enum StatusCode {
  SUCCESS = 200,
  BAD_REQUEST = 400,
  UNAUTHORIZED = 401,
  FORBIDDEN = 403,
  NOT_FOUND = 404,
  SERVER_ERROR = 500,
}

// !通用响应生成函数
function createResponse<T>(
  code: number,
  data: T | null,
  message: string,
  success: boolean
) {
  return { code, data, message, success };
}

// 各种响应类型的辅助函数
export function useSuccess<T>(data: T, message = 'Success') {
  return createResponse(StatusCode.SUCCESS, data, message, true);
}

export function useServerError(message = 'Server Error') {
  return createResponse(StatusCode.SERVER_ERROR, null, message, false);
}

export function useNotFoundError(message = 'Not Found') {
  return createResponse(StatusCode.NOT_FOUND, null, message, false);
}

export function useFail(message = 'Fail') {
  return createResponse(StatusCode.BAD_REQUEST, null, message, false);
}

export function useUnauthorized(message = 'Unauthorized') {
  return createResponse(StatusCode.UNAUTHORIZED, null, message, false);
}

export function useForbidden(message = 'Forbidden') {
  return createResponse(StatusCode.FORBIDDEN, null, message, false);
}
